package de.dopichaj.labrador.index.backend;

import java.io.DataInput;
import java.io.IOException;

import de.dopichaj.labrador.util.io.BitInputStream;
import de.dopichaj.labrador.util.io.ByteReader;


public final class TermFrequencyInputStream {

    private final BitInputStream input;
    private int previousTermID = 0;
    private int previousDocumentID = 0;
    private final boolean compressed;
    private final int assumedTermID;
    private boolean startOfRun;
    
    public TermFrequencyInputStream(final ByteReader input, final boolean compressed,
        final int assumedTermID) {

        this(new BitInputStream(input), compressed, assumedTermID);
    }

    public TermFrequencyInputStream(final BitInputStream input, final boolean compressed,
        final int assumedTermID) {

        this.input = input;
        this.compressed = compressed;
        this.assumedTermID = assumedTermID;
        this.startOfRun = true;
    }

    public TermFrequencyInputStream(final ByteReader input, final boolean compressed) {
        this(new BitInputStream(input), compressed);
    }

    public TermFrequencyInputStream(final DataInput input, final boolean compressed) {
        this(new BitInputStream(input), compressed);
    }

    public TermFrequencyInputStream(final BitInputStream input, final boolean compressed) {
        
        this(input, compressed, -1);
    }

    public TermFrequencyInputStream(final ByteReader input) {
        this(input, true);
    }
    
    public TermFrequency readTermFrequency() throws IOException {

        final TermFrequency freq;
        if (compressed) {
            
            final boolean readTermID = assumedTermID < 0;
            if (readTermID) {
                freq = readWithTermID();
            } else {
                freq = readWithoutTermFrequency();
            }
        } else {
            freq = readUncompressed();
        }
        previousDocumentID = freq != null ? freq.getDocumentID() : 0;
        startOfRun = false;
        return freq;
    }

    private TermFrequency readUncompressed() throws IOException {
        final int termID;
        final int documentID;
        final int frequency;

        termID = readBig() - 1;
        if (termID == -1) {
            return null;
        }
        documentID = readBig();
        frequency = input.readUnary();
        return new TermFrequency(assumedTermID, documentID, frequency);
    }
    
    private TermFrequency readWithoutTermFrequency() throws IOException {
        
        final int documentID;
        final int frequency;
        if (startOfRun) {
            documentID = readBig();
        } else {
            final int delta = readSmall() - 1;
            if (delta < 0) {
                return null;
            }
            documentID = previousDocumentID + delta;
        }
        frequency = input.readUnary();
        return new TermFrequency(assumedTermID, documentID, frequency);
    }
    
    private TermFrequency readWithTermID() throws IOException {
        
        int value = readSmall();
        startOfRun = value == 0;
        final int termID;
        final int documentID;
        final int frequency;
        if (startOfRun) {
        
            value = readSmall();
            if (value == 0) {
                termID = readBig() - 1;
                if (termID == -1) {
                    return null;
                }
            } else {
                termID = previousTermID + value - 1;
            }
            
            previousTermID = termID;
            documentID = readBig();
            if (documentID == -1) {
                return null;
            }
        } else {
            termID = previousTermID;
            if (value == 1) {
                documentID = readBig();
            } else {
                documentID = previousDocumentID + value - 2;
            }
        }
        frequency = input.readUnary();
        return new TermFrequency(termID, documentID, frequency);
    }
    
    private int readSmall() throws IOException {
        return input.readDelta() - 1;
    }

    private int readBig() throws IOException {
        return input.readVariableLengthInt();
    }
    
    public void skipToNext() {
        startOfRun = true;
        input.skipToNext();
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/